package webapp.mvc.models;

import com.zoowii.jpa_utils.orm.Model;
import com.zoowii.jpa_utils.util.StringUtil;
import webapp.mvc.enums.UserGenders;
import webapp.mvc.finders.UserFinder;
import webapp.mvc.utils.IdGenerator;
import webapp.mvc.utils.TimeUtil;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "user")
public class UserEntity extends Model {
    public static final UserFinder find = new UserFinder(String.class, UserEntity.class);
    
    private String id = IdGenerator.nextId();
    private String username;
    private String password;
    private int gender = UserGenders.PRIVATE;
    private String phone = "";
    private String wechatUid = "";
    private String nick = "";


    public static UserFinder getFind() {
        return find;
    }

    private String userlogo = "";
    
    private Timestamp createTime = TimeUtil.currentTimestamp();
    private Timestamp modifyTime = TimeUtil.currentTimestamp();

    // transient fields
    private List<RoleEntity> roles = new ArrayList<>();

    @Id
    @Column(name = "id", nullable = false)
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "userlogo", nullable = false)
    public String getUserlogo() {
        return userlogo;
    }

    @Column(name = "userlogo", nullable = false)
    public void setUserlogo(String userlogo) {
        if (userlogo.equals("")) {
            this.userlogo = "http://blog.bzxxg.cn/wp-content/uploads/2013/07/guest.png";
        }
        this.userlogo = userlogo;
    }

    @Column(name = "username", nullable = false)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "password", nullable = false)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "gender", nullable = false)
    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    @Column(name = "phone", nullable = false)
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column(name = "wechat_uid", nullable = false)
    public String getWechatUid() {
        return wechatUid;
    }

    public void setWechatUid(String wechatUid) {
        this.wechatUid = wechatUid;
    }

    @Column(name = "nick", nullable = false)
    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    @Column(name = "create_time", nullable = false)
    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    @Column(name = "modify_time", nullable = false)
    public Timestamp getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Timestamp modifyTime) {
        this.modifyTime = modifyTime;
    }

    @Transient
    public List<RoleEntity> getRoles() {
        return roles;
    }

    public void setRoles(List<RoleEntity> roles) {
        this.roles = roles;
    }

    public String getDisplayName() {
        return StringUtil.isEmpty(nick) ? username : nick;
    }
}
